package com.cn.medicalcare.controller.servlet.superadmin;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.AdminMapper;
import com.cn.medicalcare.pojo.Admin;
import com.cn.medicalcare.pojo.AdminPage;
import com.cn.medicalcare.pojo.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/superadmin/getadmininpageby_.json")
public class AdminInPageBy_Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String role=req.getParameter("role");
        String survival=req.getParameter("survival");
        String pagenum=req.getParameter("page");
        Admin admin=new Admin().setUsername(username).setRole(role);
        Page page=null;
        List<Admin> list;
        PrintWriter pw=resp.getWriter();
        Map<String,Object> map=new LinkedHashMap<>();
        try {
            if(survival!=null && "".equals(survival)){
                admin.setSurvival(null);
            }
            if (survival!=null && survival.length()>0){
                admin.setSurvival(Integer.valueOf(survival));
            }
            int page_num=Integer.valueOf(pagenum);
            if (page_num>0)
                page=new Page((page_num-1)*Page.PAGE_CAPACITY,Page.PAGE_CAPACITY);
            AdminPage adminPage=new AdminPage(admin,page);

            list= SqlSessionFactoryTool.DoMapper(AdminMapper.class, m->m.selectAdminInPage(adminPage));
            map.put("status","200");
            map.put("data",list);
            map.put("count",SqlSessionFactoryTool.DoMapper(AdminMapper.class, m->m.selectAdminCount(admin)));
        }catch (Exception e){
            map.put("status","500");
        }
        pw.print(JsonTool.toJson(map));
    }
}
